package com.example.server.Service;

import com.example.server.mapper.Databasemapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DatabaseService {
    @Autowired
    private Databasemapper databasemapper;
    public Map<String, Integer> selectsex() {
        int boy = databasemapper.selectbox();
        int girl = databasemapper.selectgirl();
        Map<String,Integer> map1 = new HashMap<>();
        map1.put("boy",boy);
        map1.put("girl",girl);
        return map1;
    }

    public Map<String, Integer> selecttype() {
        int city = databasemapper.selectcity();
        int ocean = databasemapper.selectocean();
        int country = databasemapper.selectcountry();
        int mountain = databasemapper.selectmountain();
        Map<String,Integer> map1 = new HashMap<>();
        map1.put("city",city);
        map1.put("ocean",ocean);
        map1.put("country",country);
        map1.put("mountain",mountain);
        return map1;
    }

    public int[] selectregister(int year) {
        int a1 = databasemapper.selectregis(year);
        int a2 = databasemapper.selectregis(year-1);
        int a3 = databasemapper.selectregis(year-2);
        int a4 = databasemapper.selectregis(year-3);
        int a5 = databasemapper.selectregis(year-4);
        int[] b = new int[]{a5,a4,a3,a2,a1};
        return b;
    }

    public List<Map<String, Object>> selectprovince() {
        return databasemapper.selectprovince();
    }

    public Map<String, Object> selectprovinceall() {//这里也十分精彩，前端需要一对一，所以后端直接进行数据处理，将其拆开。
        List<Map<String, Object>> cityData = databasemapper.selectprovinceall();
        List<String> cities = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (Map<String, Object> row : cityData) {
            cities.add((String) row.get("city")); // 提取城市名
            counts.add(((Number) row.get("count_as")).intValue()); // 提取数量
        }
        // 返回 JSON 格式的数据，分开存储
        Map<String, Object> result = new HashMap<>();
        result.put("cities", cities);
        result.put("counts", counts);
        return result;
    }

    public Map<String, Object> selectfar() {
        List<Map<String,Object>> farData = databasemapper.selectfar();
        List<String> cities = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (Map<String, Object> row : farData) {
            cities.add((String) row.get("city")); // 提取城市名
            counts.add(((Number) row.get("count_as")).intValue()); // 提取数量
        }
        // 返回 JSON 格式的数据，分开存储
        Map<String, Object> result = new HashMap<>();
        result.put("cities", cities);
        result.put("counts", counts);
        return result;
    }

    public Map<String, List> selectreview() {
        List<Map<String,Object>> farData = databasemapper.selectreview();
        List<String> cities = new ArrayList<>();
        List<Integer> counts = new ArrayList<>();
        for (Map<String, Object> row : farData) {
            cities.add((String) row.get("city")); // 提取城市名
            counts.add(((Number) row.get("count_as")).intValue()); // 提取数量
        }
        // 返回 JSON 格式的数据，分开存储
        Map<String, List> result = new HashMap<>();
        result.put("cities", cities);
        result.put("counts", counts);
        return result;
    }
}
